package com.ebt.m.wiki.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

public class ImagePagerAdapter extends PagerAdapter {

    private ArrayList<String> images;
    private Context mContext;

    public ImagePagerAdapter(Context context, ArrayList<String> images) {
        this.images = images;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup view, int position) {
        View viewLayout = LayoutInflater.from(mContext).inflate(R.layout.viewpager_item, view, false);
        PhotoView imageView = (PhotoView) viewLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) viewLayout.findViewById(R.id.loading);
        ImageLoaderUtil.getInstance().loadImage(AppContext.getInstance(), new ImageLoader.Builder().url(images.get(position)).imgView(imageView).build());
//        ImageLoader.getInstance().displayImage(images.get(position), imageView,
//                new SimpleImageLoadingListener() {
//                    @Override
//                    public void onLoadingStarted(String imageUri, View view) {
//                        spinner.setVisibility(View.VISIBLE);
//                    }
//
//                    @Override
//                    public void onLoadingFailed(String imageUri, View view,
//                                                FailReason failReason) {
//                        String message = null;
//                        // 获取图片失败类型
//                        switch (failReason.getType()) {
//                            case IO_ERROR: // 文件I/O错误
//                                message = "网络异常";
//                                break;
//                            case DECODING_ERROR: // 解码错误
//                                message = "解码错误";
//                                break;
//                            case NETWORK_DENIED: // 网络延迟
//                                message = "网络延迟";
//                                break;
//                            case OUT_OF_MEMORY: // 内存不足
//                                message = "内存不足";
//                                break;
//                            case UNKNOWN: // 原因不明
//                                message = "未知原因";
//                                break;
//                        }
//                        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
//                        spinner.setVisibility(View.GONE);
//                    }
//                    @Override
//                    public void onLoadingComplete(String imageUri,
//                                                  View view, Bitmap loadedImage) {
//                        spinner.setVisibility(View.GONE); // 不显示圆形进度条
//                    }
//                });

        ((ViewPager) view).addView(viewLayout, 0); // 将图片增加到ViewPager
        return viewLayout;
    }

    @Override
    public void finishUpdate(View container) {
    }

}